using System;
using System.Diagnostics;
using System.IO;
using Microsoft.SharePoint.Administration;

namespace SPDevExplorer.WebServices
{
    public class ServicesFeatureHandler : Microsoft.SharePoint.SPFeatureReceiver
    {
        private static string _serviceContract = "<contractRef  ref=<% SPHttpUtility.AddQuote(" +
                                                 "SPHttpUtility.HtmlEncode(spWeb.Url + '/_vti_bin/{0}.asmx?wsdl\')," +
                                                 "Response.Output); %> docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + '/_vti_bin/{0}.asmx')," +
                                                 "Response.Output); %> xmlns='http://schemas.xmlsoap.org/disco/scl/' />" +
                                                 "<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + '/_vti_bin/{0}.asmx?disco')," +
                                                 "Response.Output); %> xmlns='http://schemas.xmlsoap.org/disco/' />";

        public override void FeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureDeactivating(Microsoft.SharePoint.SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(Microsoft.SharePoint.SPFeatureReceiverProperties properties)
        {
            try
            {
                Microsoft.Win32.RegistryKey k =
                    Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                        "SOFTWARE\\Microsoft\\Shared Tools\\Web Server Extensions\\12.0");
                string sLocation = (string) k.GetValue("Location");
                k.Close();

                string sSPDisco = System.IO.File.ReadAllText(sLocation + "ISAPI\\spdisco.aspx");
                if (!sSPDisco.Contains("devservices.asmx?disco"))
                {
                    Trace.WriteLine("SPDevExplorer: Updating disco file");
                    sSPDisco = sSPDisco.Replace("</discovery>",
                                                string.Format(_serviceContract, "devservices") + "</discovery>");

                    using (FileStream fs = System.IO.File.OpenWrite(sLocation + "ISAPI\\spdisco.aspx"))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.Write(sSPDisco);
                            sw.Close();
                        }
                        fs.Close();
                    }
                }

                Trace.WriteLine("SPDevExplorer: ApplyApplicationContentToLocalServer");
                SPFarm.Local.Services.GetValue<SPWebService>().ApplyApplicationContentToLocalServer();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }
        }

        public override void FeatureUninstalling(Microsoft.SharePoint.SPFeatureReceiverProperties properties)
        {
        }
    }
}